package com.sysman.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.sysman.HttpTools.DataAnalyze;
import com.sysman.HttpTools.DataAnalyzeDetails;
import com.sysman.HttpTools.HttpRequest;
import com.sysman.po.UserinfoCustom;
import com.sysman.po.Userlogininfo;
import com.sysman.service.PatientService;

/** 
* @author  LTT-AH-ZY E-mail: 
* @date 创建时间：2017年8月8日 上午10:33:10 
* @version 1.0 
* @parameter 参数及其意义
* @since 最早使用该方法/类/接口的JDK版本 
* @since  
* @return  
*/
@Controller
@RequestMapping(value="/Main/Patient")
public class PatientController {
	/**
	 *	注入PatientService服务，实现对dao层的数据库操作，实现列表查询
	 */
	@Autowired
	private PatientService patientService;
	
	/**
	 * 这里是病人端列表初始化界面，实现对所有病人端注册用户（包括正式用户和非正式用户）的管理
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/patientlist")
	public ModelAndView patientlist() throws Exception {
		//List<UserlogininfoCustom> UserlogininfoList = patientService.InitUserlogininfoList(null);
		ModelAndView model  = new ModelAndView();
		//model.addObject("userlogininfolist", UserlogininfoList);
		model.setViewName("/View/PatientMan/PatientList");
		return model;
	}
	/**
	 * 病人端列表动态加载
	 * @param limit
	 * @param offset
	 * @param type
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/patientListAjax")
	public @ResponseBody Map<String,Object> patientListAjax(Integer limit,Integer offset,Integer type,Integer useradrprovince,Integer useradrcity,Integer useradrarea,Integer userage,String userloginphone,String startdatetime,String enddatetime,String usermale,HttpServletRequest request)throws Exception{
		HttpSession session=request.getSession();
		Integer adminloginid = (Integer)session.getAttribute("adminloginid");
		if(userage==null) {
			userage=0;
		}
		System.out.println("输出接收的参数是："+limit+"//"+offset+"//"+type+"//"+adminloginid);
		String PatientParam = "limit="+limit+"&offset="+offset+"&type="+type+"&adminloginid="+adminloginid+"&useradrprovince="+useradrprovince+"&useradrcity="+useradrcity+"&useradrarea="+useradrarea+"&userage="+userage+"&userloginphone="+userloginphone+"&startdatetime="+startdatetime+"&enddatetime="+enddatetime+"&usermale="+usermale;//参数串
		System.out.println("输出参数串："+PatientParam);
		List<Map<String,Object>> jsonlist=new ArrayList<>();;
		Integer total=0;
		String code="";
		String msg="";
		Map<String, Object> map = new HashMap<String, Object>();  
		if(adminloginid==null) {
			code="-100";
			msg="请先登录";
		}else{
			try {
				JSONObject PatientListjson = HttpRequest.sendPost("listuser", PatientParam);
				System.out.println("输出接收到的json数据是："+PatientListjson);
				
				msg = PatientListjson.get("msg").toString();
				code =PatientListjson.get("code").toString();
				if("100".equals(code)) {
					try {
						JSONObject jsonobjectdata =PatientListjson.getJSONObject("data");
						JSONArray jsonarrayrows = jsonobjectdata.getJSONArray("rows");
						total = Integer.parseInt(jsonobjectdata.get("total").toString());
						jsonlist = DataAnalyze.PatientJsonToList(jsonarrayrows);
						
					} catch (Exception e) {
						System.out.println("出错信息是："+e.getMessage());
				        code="-300";
						msg="数据解析出错";
					}
				}
			} catch (Exception e) {
				System.out.println("出错信息是："+e.getMessage());
		        code="-200";
				msg="数据传输出错";
			}
		}
		map.put("msg", msg);
		map.put("rows", jsonlist);
		map.put("code", code);
        map.put("total", total);
		System.out.println("输出这个map："+map);
		return map;
	}
	
	/**
	 * 这里是病人端审核界面的初始化类，实现对所有符合要求的数据进行查询 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/patientexamine")
	public ModelAndView patientexamine() throws Exception{
		//List<UserlogininfoCustom> UserlogininfoExamineList = patientService.InitUserlogininfoExamineList(null);
		ModelAndView model = new ModelAndView();
		//model.addObject("UserlogininfoExamineList", UserlogininfoExamineList);
		model.setViewName("View/PatientMan/PatientExamine");
		return model;
	}
	/**
	 * 实现一键审核，从前台接收传输过来的id值，进行updateById，改变userlogintype状体
	 * @param userloginids
	 * @param userlogininfoCustom
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/patientexamineSubmit")
	public @ResponseBody Map<String,Object> patientexamineSubmit(HttpServletRequest request) throws Exception{
		HttpSession session=request.getSession();
		Integer adminloginid = (Integer)session.getAttribute("adminloginid");
		
		String userloginid = request.getParameter("userloginid");
		String isagree = request.getParameter("isagree");
		boolean type = Boolean.parseBoolean(isagree);
		String idea = request.getParameter("idea");
		String Param = "adminloginid="+adminloginid+"&userloginid="+userloginid+"&type="+type+"&idea="+idea;
		System.out.println("参数Param是："+Param);
		Map<String, Object> ExamineJson = new HashMap<>();
		String code="";
		String msg="";
		
		if(adminloginid!=null && userloginid!=null && isagree!=null){
			try {
				JSONObject json = HttpRequest.sendPost("checkuser",Param);
				System.out.println("服务器返回json信息："+json);
		        code = json.get("code").toString();
		        msg = json.get("msg").toString();
			} catch (Exception e) {
				System.out.println("出错了，错误信息是："+e.getMessage());
				code="-200";
				msg="数据传输错误";
			}
			
		}else {
			code="-100";
			msg="审核提交失败，检查提交数据！";
		}
		ExamineJson.put("code", code);
		ExamineJson.put("msg", msg);
		return ExamineJson;
	}
	/**
	 * 根据前台传输的数据，实现selectById，并将结果返还给前台
	 * 自定义查询，表外键关联查询，实现对外键表的信息查询
	 * @param id
	 * @param userinfoCustom
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/patientdetails" , method = { RequestMethod.POST,RequestMethod.GET })
	public ModelAndView patientdetails(@RequestParam(value = "id", required = true) Integer id,UserinfoCustom userinfoCustom) throws Exception {
		System.out.println("输出ID是"+id);
		userinfoCustom.setUserloginid(id);
		System.out.println("输出这个userinfo里的ID"+userinfoCustom.getUserloginid());
		UserinfoCustom userinfodetails = patientService.finduserinfoDetails(userinfoCustom);
		Userlogininfo userlogininfodetails = patientService.finduserlogininfoDetails(id);
		ModelAndView model = new ModelAndView();
		model.setViewName("/View/PatientMan/PatientDetails");
		model.addObject("userinfodetails", userinfodetails);
		model.addObject("userlogininfodetails",userlogininfodetails);
		return model;
	}
	/**
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/getPatientDetails")
	public @ResponseBody Map<String,Object> getPatientDetails(HttpServletRequest request)throws Exception{
		HttpSession session=request.getSession();
		Integer adminloginid = (Integer)session.getAttribute("adminloginid");
		
		String userloginid = request.getParameter("userloginid");
		String patientparam = "adminloginid="+adminloginid+"&userloginid="+userloginid;
		Map<String,Object> jsontomap  =new HashMap<String,Object>();
		Map<String, Object> map = new HashMap<String, Object>(); 
		String code="";
		String msg="";
		if(userloginid==null || adminloginid==null){
			msg="请先登录";
			code="-100";
		}else {
			try {
				JSONObject PatientDeatilsjson = HttpRequest.sendPost("getuserdetail", patientparam);
				System.out.println("输出接收到的json数据是："+PatientDeatilsjson);
				
				msg = PatientDeatilsjson.get("msg").toString();
				code =PatientDeatilsjson.get("code").toString();
				try {
					jsontomap = DataAnalyzeDetails.PatientJsonToDetails(PatientDeatilsjson.getJSONObject("data"));
				} catch (Exception e) {
					System.out.println("出错信息是："+e.getMessage());
					msg="数据解析错误";
					code="-300";
				}
			} catch (Exception e) {
				System.out.println("出错信息是："+e.getMessage());
				msg="数据请求错误";
				code="-200";
			}
		}
		map.put("msg", msg);
		map.put("code", code);
		map.put("details", jsontomap);
		return map;
	}
	/**
	 * 获取城市下拉框
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/loadcity")
	public @ResponseBody Map<String,Object> loadcity(HttpServletRequest request)throws Exception{
		Integer cityparentcode = Integer.parseInt(request.getParameter("cityparentcode"));
		String param = "cityparentcode="+cityparentcode;
		System.out.println("输出参数串param："+param);
		Map<String,Object> Provincemap = new HashMap<String,Object>();
		List<Map<String,Object>> list = new ArrayList<>();
		String code="";
		String msg="";
		try {
			JSONObject provincejson = HttpRequest.sendPost("listcity", param);
			code = provincejson.get("code").toString();
			msg = provincejson.get("msg").toString();
			if("100".equals(code)) {
				try {
					JSONArray data = provincejson.getJSONArray("data");
					for(int i=0;i<data.length();i++) {
						Map<String,Object> map = new HashMap<>();
						JSONObject job = data.getJSONObject(i);
						map.put("citycode", job.get("citycode").toString());
						map.put("cityname", job.get("cityname").toString());
						list.add(map);
					}
				} catch (Exception e) {
					code="-200";
					msg="数据解析错误";
					System.out.println("出错信息是："+e.getMessage());
				}
			}
		} catch (Exception e) {
			code="-100";
			msg="数据传输错误";
			System.out.println("出错信息是："+e.getMessage());
		}
		Provincemap.put("code", code);
		Provincemap.put("msg", msg);
		Provincemap.put("list", list);
		return Provincemap;
	}
	/**
	 * 病人信息修改提交
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/patientinfoSubmit")
	public @ResponseBody Map<String,Object> patientinfoSubmit(HttpServletRequest request)throws Exception{
		HttpSession session = request.getSession();
		Integer adminloginid=(Integer)session.getAttribute("adminloginid");
		String username=request.getParameter("username");
		String userage=request.getParameter("userage");
		String usermale=request.getParameter("usermale");
		String usercardnum=request.getParameter("usercardnum");
		String useradrprovince=request.getParameter("useradrprovince");
		String useradrcity=request.getParameter("useradrcity");
		String useradrarea=request.getParameter("useradrarea");
		String useradrother=request.getParameter("useradrother");
		String param="adminloginid="+adminloginid+"&username="+username+"&userage="+userage+"&usermale="+usermale+"&usercardnum"+usercardnum+
				"&useradrprovince="+useradrprovince+"&useradrcity="+useradrcity+"&useradrarea="+useradrarea+"&useradrother="+useradrother;
		Map<String,Object> result = new HashMap<String,Object>();
		String code="";
		String msg="";
		try {
			JSONObject json = HttpRequest.sendPost("editpatientinfo", param);
			code=json.get("code").toString();
			msg=json.get("msg").toString();
			
		} catch (Exception e) {
			code="-100";
			msg="数据传输错误";
			System.out.println("出错了，错误信息是："+e.getMessage());
		}
		result.put("code", code);
		result.put("msg", msg);
		
		return result;
	}
}
